$(document).ready(function() {

    var url = request.context + '/extract/config.html';
    
    var showFileLocation = function() {
        var locId = $(this).val();
        $('.locations .location').hide();
        $('.locations .location').each(function(){
            if ($(this).hasClass('location-' + locId)) {
                var sizes = $(this).find('.location-file label').map(function() {
                    return $(this).text().length;
                }).get()
                .sort(function(a, b) {
                    return b - a;
                });
                if (sizes.length > 0) {
                    var sizeMax = sizes[0];
                    sizeMax = sizeMax / 2 + 1;
                    $(this).find('.location-file').css('width', sizeMax + 'em')
                }
                $(this).show();
            } else {
                $(this).hide();
                $(this).find('input:checked').attr('checked', false);
            }
        });
    };
    
    var addFileLocation = function() {

        var selectNewLocation = function(data) {
            if (!data || !data.id || !data.location) {
                alert('Invalid server response: ' + jQuery.serialize(data));
            }
            var id = data.id;
            var location = data.location;
            $('.locations .location-select').append(
                $('<option></option>')
                .attr('value', id)
                .text(location));
            var ol = $('<ol></ol>').addClass('location-file-list');
            var files = data.files;
            $.each(files, function(index, value) {
                ol.append(
                    $('<li></li>')
                    .addClass('location-file')
                    .addClass('location-file-' + id)
                    .append(
                        $('<input type="checkbox">')
                        .attr('name', id)
                        .attr('value', value))
                    .append(
                        $('<label></label>')
                        .addClass('location-file-name')
                        .text(value)));
            });
            $('.locations').append(
                $('<div></div>')
                .addClass('location')
                .addClass('location-' + id)
                .append(ol));
            $('.locations .location-select').val(id).change();
        };

        var callback = function(data) {
            if (data.status == 'OK') {
                selectNewLocation(data);
            } else {
                alert(data.message);
            }
        }

        var newLocation = prompt('Path to a new location', '');
        if (newLocation) {
            var data = [{
                'name': 'newLocation',
                'value': newLocation
            }];
            jQuery.post(url, data, callback, 'json')
        }
    };

    $('.location-add').click(addFileLocation);
    $('.location-select').change(showFileLocation);
    $('.location-select').change();

});
